package com.zHan.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zHan.sky.common.content.BaseContext;
import com.zHan.sky.dto.ShoppingCartDTO;
import com.zHan.sky.entity.Dish;
import com.zHan.sky.entity.Setmeal;
import com.zHan.sky.entity.ShoppingCart;
import com.zHan.sky.mapper.DishMapper;
import com.zHan.sky.mapper.SetmealMapper;
import com.zHan.sky.mapper.ShoppingCartMapper;
import com.zHan.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 创建空购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        // 对象拷贝
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 设置userId
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 判断商品是否存在
        Long userId = shoppingCart.getUserId();
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        String dishFlavor = shoppingCart.getDishFlavor();
        // 条件查询
        List<ShoppingCart> shoppingCartList = lambdaQuery().eq(userId != null,
                        ShoppingCart::getUserId, userId)
                .eq(dishId != null, ShoppingCart::getDishId, dishId)
                .eq(setmealId != null, ShoppingCart::getSetmealId, setmealId)
                .eq(StringUtils.hasText(dishFlavor), ShoppingCart::getDishFlavor, dishFlavor)
                .list();
        if (!ObjectUtils.isEmpty(shoppingCartList)) {
            // 已经存在，数量+1，修改数据
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setNumber(cart.getNumber() + 1);
            updateById(cart);
        } else {
            // 不存在，新增数据
            // 判断添加的是菜品还是套餐
            if (dishId != null) {
                // 菜品
                Dish dish = dishMapper.selectById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                // 套餐
                Setmeal setmeal = setmealMapper.selectById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            // 设置数量和创建时间
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 不存在，新增一条sql语句
            save(shoppingCart);
        }
    }

    /**
     * 删除购物车中一个商品
     *
     * @param shoppingCart
     */
    @Override
    public void sub(ShoppingCart shoppingCart) {
        //条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //过滤条件
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                .orderByDesc(ShoppingCart::getCreateTime);

        //判断是菜品还是套餐
        //是菜品
        if (shoppingCart.getDishId() != null) {
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart shoppingCartOne = getOne(queryWrapper);
        //判断number等不等于1
        Integer number = shoppingCartOne.getNumber();
        //number等于1
        if (number == 1) {
            //商品不存在，要删除该该商品
            this.removeById(shoppingCartOne.getId());
        } else {
            //数量减一
            shoppingCartOne.setNumber(number - 1);
            this.updateById(shoppingCartOne);
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        this.remove(queryWrapper);
    }

}
